﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Anyask
{
    public partial class chat : Form
    {
        private List<String> cachedMessages = new List<String>();

        public chat()
        {
            InitializeComponent();
        }

        public void sendMessage()
        {
            String tempText = this.InputBox.Text;// this.InputBox.Rtf;
            this.InputBox.Text = "";

            if (tempText.Length == 0) return;

            cachedMessages.Add(tempText);
            try
            {
                //OutputBox.Document.Body.Document.Write(tempText);
                String timeString = DateTime.Now.ToLocalTime().ToString("HH:mm:ss");
                String[] ps = new String[] { "Solomon", tempText, timeString };
                this.OutputBox.Document.InvokeScript("sendMessage", ps);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
            }
            //this.OutputBox.Text = "<font color=red>"+this.InputBox.Text+"</font>" + "\n" + this.OutputBox.Text;

            this.InputBox.Select();
        }

        private void ButtonSend_Click(object sender, EventArgs e)
        {
            this.sendMessage();
        }

        private void chat_Load(object sender, EventArgs e)
        {
            this.whenOffline();
            //this.ResetLayout();
            //this.OutputBox.Document.Body.InnerHtml = "WOLF";
            this.OutputBox.Navigate(Properties.Settings.Default.CHAT_CONTENT_DEFAULT_URL);
            //this.Text = "李颢 - AnyAsk";
        }

        private void InputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.S && e.Alt == true) || (e.KeyCode == Keys.Enter && (e.Control != true && e.Shift != true)))
            {
                this.sendMessage();
                e.SuppressKeyPress = true;
            }
        }

        private void whenOffline()
        {
            InputBox.Enabled = false;
            ButtonSend.Enabled = false;
        }

        private void whenOnline()
        {
            InputBox.Enabled = true;
            ButtonSend.Enabled = true;
            this.InputBox.Focus();
        }

        private void OutputBox_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            whenOnline();
        }


    }
}
